package thread;

import static java.lang.Thread.sleep;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Ap0stoL2
 * Date: 2023-07-24
 * Time: 21:52
 */
//各种状态
public class Demo11 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{

            while(!Thread.currentThread().isInterrupted()){ //当没捕获到异常,也就是刚好sleep结束的时候,标记位又为终止则退出
                System.out.println("i am thread");
                try {
                    sleep(1000);
                    //System.out.println(Thread.currentThread().getState()); //runnable
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break; //当捕获到异常后跳出
                }
            }
        });

        System.out.println(t.getState()); //NEW

        t.start();
        System.out.println(t.getState()); //runnable
        sleep(5000);
        System.out.println("线程结束");
        t.interrupt();

        System.out.println(t.getState()); //TERMINATED

    }
}
